home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 7_10.lha / 7_10 / qh_splice.c < prev    next >
Text File  |  1993-08-08  |  2KB  |  58 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. include <process.h>
  6. include <debug.h>    /* DELETE */
  7. * this qhead is supposed to be upstream to the qtail old_tail
  8.   add the contents of this's queue to old_tail's queue
  9.   destroy this, old_tail, and this's queue
  10.   alert the spliced qhead and qtail if a significant state change happened
  11. /
  12. oid queue_head::splice(queue_tail *old_tail)
  13.  
  14.    if (debug) /*DELETE*/ cerr << "queue_head" << this << "::splice(" << old_tail << ")\n";
  15.    process_queue *tailsQ = old_tail->qt_queue;
  16.    process_queue *headsQ = qh_queue;
  17.    int tails_count = tailsQ->q_count;
  18.    int heads_count = headsQ->q_count;
  19.    tailsQ->q_count = 0;
  20.  
  21.    /* either merge the two lists ... */
  22.    if (heads_count)
  23. {
  24. if (tails_count)
  25.     {
  26.     process_object *heads_last = headsQ->q_last;
  27.     process_object *tails_last = tailsQ->q_last;
  28.     process_object *tails_first = tails_last->po_next;
  29.     tails_last->po_next = heads_last->po_next;
  30.     heads_last->po_next = tails_first;
  31.     headsQ->q_count += tails_count;
  32.     }
  33. }
  34.  
  35.    /* ... or move the tail's queue to the head */
  36.    else
  37. {
  38. headsQ->q_count = tails_count;
  39. headsQ->q_last = tailsQ->q_last;
  40. tailsQ->q_last = 0;
  41. }
  42.  
  43.    /* patch tail's head onto head's queue */
  44.    headsQ->q_head = tailsQ->q_head;
  45.    tailsQ->q_head->qh_queue = headsQ;
  46.    tailsQ->q_head = 0;
  47.    qh_queue = 0;
  48.  
  49.    /* delete the throwaways */
  50.    delete old_tail;
  51.    delete this;
  52.  
  53.    /* alert the head if its queue becomes non-empty */
  54.    if ((tails_count == 0) && (heads_count > 0))
  55. headsQ->q_head->alert();
  56.    if (debug) /*DELETE*/ cerr << "<<<< qhead" << this << "::splice(" << old_tail << ")\n";
  57.  
  58.